#light

module Dwarf_GraphModels_Generator

open System
open System.IO
open System.Text
open System.ComponentModel
open System.Globalization;

let Dwarf_GraphModels_Generator (fs : FileStream, args : String[]) = 

    let sw = new StreamWriter(fs);
    let rand = new Random();
    
    
    // current data values
    let number_of_observation = (int) args.[args.Length - 1];
    let mutable number_of_states = 2 * number_of_observation;    
    let mutable length_of_observation_sequence = number_of_observation;
    
    for i = 0 to args.Length - 2 do     // search for user-defined parameters
        if (args.[i] = "-states") then number_of_states <- (int) args.[i + 1]
        if (args.[i] = "-sequence") then length_of_observation_sequence <- (int) args.[i + 1]

    sw.Write("M:"); 
    //write the number of Observation symbols
    sw.WriteLine(number_of_observation);
    
    sw.Write("N:");
    //write the number of states 
    sw.WriteLine(number_of_states);
    
    sw.WriteLine("A:");
    
    let ci = new CultureInfo("en-us");
    
    // write state transition matrix row by row, each element is double separated by " " simbol.
    for i = 0 to number_of_states - 1 do
        for j = 0 to number_of_states - 1 do
           do sw.Write((rand.NextDouble() / (float)number_of_states).ToString("0.0E0",ci)); 
           do if (j <> (number_of_states - 1)) then sw.Write(' ');
        do sw.WriteLine();
    
    sw.WriteLine("B:");
    
    //write odservation probability matrix row by row, each element is double separated by " " simbol.
    for i = 0 to number_of_states - 1 do
        for j = 0 to number_of_observation - 1 do
            do sw.Write((rand.NextDouble() / (float)number_of_observation).ToString("0.0E0",ci)); 
            do if (j <> (number_of_observation - 1)) then sw.Write(' ');
        do sw.WriteLine();

    sw.WriteLine("pi:");

    //write initial states list, each element is double separated by " " simbol.
    for i = 0 to number_of_states - 1 do
        do sw.Write((rand.NextDouble() /  (float)number_of_states).ToString("0.0E0",ci));
        do if (i <> (number_of_states - 1)) then sw.Write(' '); 
    do sw.WriteLine();
    
    sw.Write("T:");
    // write the Length of observation sequence.
    sw.WriteLine(length_of_observation_sequence);

    sw.WriteLine("O:");
    // write Observation sequence, each element is integer separated by " " simbol.
    for i = 0 to length_of_observation_sequence - 1 do
        do sw.Write(rand.Next(number_of_observation) );
        do if (i <> (length_of_observation_sequence - 1)) then sw.Write(' '); 
    
    sw.Close();
    true
    
